package com.neulogistics.contrib.warehouse.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.warehouse.api.application.command.UpdateInventoryCommand;
import com.neulogistics.contrib.warehouse.domain.model.aggregate.Inventory;
import com.neulogistics.contrib.warehouse.domain.repository.IInventoryRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UpdateInventoryCommandHandler implements IRequestHandler<UpdateInventoryCommand, BaseResponse<Boolean>> {
    @Resource
    IInventoryRepository inventoryRepository;

    @Override
    public BaseResponse<Boolean> handle(UpdateInventoryCommand command) throws Exception {
        Inventory inventory = inventoryRepository.findById(command.inventoryId);

        inventory.setMaxHold(command.maxHold);
        inventory.setWarnHold(command.warnHold);

        inventoryRepository.save(inventory);
        return ResultUtils.success(Boolean.TRUE);
    }
}
